/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package negocios;

import acceso_datos.EmergenciaDB;
import acceso_datos.TipoEmergenciaDB;
import datos.WEmergenciaInicial;
import exceptions.AlfanumericoException;
import exceptions.Alfanumero;
import exceptions.EmergenciaInicialException;
import java.sql.SQLException;
import java.util.HashMap;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author miguesaca
 */
public class WGestorEmergenciaInicial {

    private WEmergenciaInicial emergenciaInicial;
    private HashMap<Integer, WEmergenciaInicial> listaEmergenciaInicial = new HashMap<Integer, WEmergenciaInicial>();
    private Alfanumero alfaNumero;
    public WGestorEmergenciaInicial() throws SQLException {
        alfaNumero = new Alfanumero();
        cargarDatos();
    }

    public WEmergenciaInicial getEmergenciaInicial() {
        return emergenciaInicial;
    }

    public void setEmergenciaInicial(WEmergenciaInicial emergenciaInicial) {
        this.emergenciaInicial = emergenciaInicial;
    }

    public void cargarDatos() throws SQLException {
        
        EmergenciaDB edb=new EmergenciaDB();
//        listaEmergenciaInicial=edb.listar();
        //listaEmergenciaInicial.put(emergenciaInicial.getRegistroLlamada().getId(), emergenciaInicial);
    }

    public void insertar(WEmergenciaInicial emergenciaInicial) throws EmergenciaInicialException, AlfanumericoException, SQLException {
        alfaNumero.vacio(emergenciaInicial.getZona());
        alfaNumero.vacio(emergenciaInicial.getDescripcionGeneral());
//        if (listaRegistroLlamadas.containsKey(emergenciaInicial.getRegistroLlamada().getClave())) {
//            throw new EmergenciaInicialException("Emergencia Inicial ya existe");
//        }
        this.emergenciaInicial = emergenciaInicial;
        
        
        TipoEmergenciaDB tedb=new TipoEmergenciaDB();
        emergenciaInicial.getTipoEmergencia().setId(tedb.getMaxId()+1);        
        tedb.grabar(emergenciaInicial.getTipoEmergencia());
                
        
        EmergenciaDB edb=new EmergenciaDB();
        emergenciaInicial.setId(edb.indiceFinal()+1);        
//        edb.grabar(emergenciaInicial);        
        
        listaEmergenciaInicial.put(emergenciaInicial.getId(), emergenciaInicial);
    }

    public void modificar(WEmergenciaInicial emergenciaInicial) throws EmergenciaInicialException {
        buscar(emergenciaInicial.getRegistroLlamada().getId());
        this.emergenciaInicial = emergenciaInicial;
        listaEmergenciaInicial.put(emergenciaInicial.getId(), emergenciaInicial);
    }

    public WEmergenciaInicial buscar(int clave) throws EmergenciaInicialException {
        if (!listaEmergenciaInicial.containsKey(clave)) {
            throw new EmergenciaInicialException("Emergencia no Existente");
        }
        return listaEmergenciaInicial.get(clave);
    }

    public void eliminar(int clave) throws EmergenciaInicialException {
        buscar(clave);
        listaEmergenciaInicial.remove(clave);
    }

    public DefaultTableModel tablaModelo() {
        Object nameColumns[] = {"Clave","Zona","DescripcionGeneral"};
        DefaultTableModel miTableModel = new DefaultTableModel(nameColumns, 0);
        for (WEmergenciaInicial value : listaEmergenciaInicial.values()) {
            Object nuevaFila[] = {value.getId(),value.getZona(),value.getDescripcionGeneral()};
            miTableModel.addRow(nuevaFila);
        }

        return miTableModel;
    }
}
